package com.ZeroCarbon.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class EmailService {

    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 发送邮件
     */
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 发送邮件
     */
    public void sendSimpleEmail(String to, String subject, String text) {
        //定制纯文本邮件信息SimpleMailMessage
        SimpleMailMessage message = new SimpleMailMessage();
        //设置发送者
        message.setFrom(from);
        //设置接受者
        message.setTo(to);
        //设置标题
        message.setSubject(subject);
        //设置标题
        message.setText(text);
        try {
            //发送邮件
            mailSender.send(message);
        } catch (MailException e) {
            log.error(e.toString());
        }
    }
}
